<?php
class LevelController extends AdminBaseController {
	public function _initialize() {
		parent::_initialize();
		//$this->db = model('League');
	}


	public function index(){
		if(IS_POST){
			// p($_POST);
			$file="maxhas.php";
			// die;
			if (file_exists(CONF_PATH . $file)){
				if ($this->update_config(daddslashes($_POST), CONF_PATH . $file)) {
					showmessage('操作成功', '', -1);
				} else {
					showmessage('操作失败');
				}	
			}else{
				$conf_file=file_put_contents(CONF_PATH . $file, '<?php return array(); ?>');
				if ($this->update_config(daddslashes($_POST), CONF_PATH . $file)) {
					showmessage('操作成功', '', -1);
				} else {
					showmessage('操作失败');
				}	
			}
		}else{
			include $this->admin_tpl('level');
		}
	}
	public function miner(){
		if(IS_POST){
			// p($_POST);
			$file="miner.php";
			// die;
			if (file_exists(CONF_PATH . $file)){

				foreach ($_POST['miner'] as $key => $value) {
					$_POST['miner'][$key]['secmin'] = bcdiv($value['profit24min'], 86400,8);
					$_POST['miner'][$key]['secmax'] = bcdiv($value['profit24max'], 86400,8);
				}


				if ($this->update_config(daddslashes($_POST), CONF_PATH . $file)) {
					showmessage('操作成功', '', -1);
				} else {
					showmessage('操作失败');
				}	
			}else{
				$conf_file=file_put_contents(CONF_PATH . $file, '<?php return array(); ?>');
				if ($this->update_config(daddslashes($_POST), CONF_PATH . $file)) {
					showmessage('操作成功', '', -1);
				} else {
					showmessage('操作失败');
				}	
			}
		}else{
			include $this->admin_tpl('miner');
		}
	}




	/* 保存配置文件 */
	private function update_config($config, $config_file = '') {
        if(empty($config_file)) return false;
        if($fp = @fopen($config_file, 'wb')) {
            fwrite($fp, "<?php \nreturn " . stripslashes(var_export($config, true)) . ";");
            fclose($fp);
            return true;
        } else {
            return false;
        }
    }







}

/*function getImgs($content,$order='ALL'){
	  
		$pattern="/<img src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg]))[\'|\"].*?[\/]?>/";
		preg_match_all($pattern,$content,$match);
		return $match[1];
			
	}*/